package task2.util

import org.apache.spark.internal.Logging

object OptionsParser extends Logging{

  def parse(args: Array[String]): SparkDistcpOption = {
    logInfo(s"接收参数：${args.toSeq}")
    require(args.length >= 2 && args.length <= 4, "usage: sparkDistCp -i -m xxx hdfs://xxx/source hdfs://xxx/target")
    // 解析 -i
    val ignoreFailures = args.exists(_.equals("-i"))
    // 解析 -m
    val x = args.find(_.startsWith("-m")).getOrElse("-m 1")
    val maxConcurrence = x.substring(x.lastIndexOf(" ") + 1).toInt
    // 解析文件路径
    val source = args(args.length - 2)
    val target = args(args.length - 1)
    SparkDistcpOption(ignoreFailures, maxConcurrence, source, target)
  }

}

case class SparkDistcpOption(ignoreFailures: Boolean, maxConcurrence: Int, source: String, target: String)
